package mavenWebProject;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/**
 * Servlet implementation class LoadInServlet
 */
@WebServlet("/LoadInServlet")
public class LoadInServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoadInServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		//1.加载mybatis的核心配置文件，获取sqlsessionfactory
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		
		//2.获取sqlsession对象，用它来执行sql
		SqlSession sqlsession=sqlSessionFactory.openSession();
		
		//3.获取mapper接口的代理对象
		userMapper mapper=sqlsession.getMapper(userMapper.class);
		
		//4.执行方法
		request.setCharacterEncoding("UTF-8");
		String username = request.getParameter("inputUsername");
		String password = request.getParameter("inputPassword");
		List<user> user=mapper.selectByAccount(username.trim(),password.trim());
		if(!user.isEmpty()) {
			HttpSession usernameSession=request.getSession();
			usernameSession.setAttribute("username",username);
			HttpSession identitySession=request.getSession();
			String identityForSession="";
			for(user s:user) {identityForSession=s.getAccountIdentity().trim();}
			identitySession.setAttribute("identity",identityForSession);
			String identity="";
			for(user s:user) {
				identity=s.getAccountIdentity().trim();
			}
			if(identity.contentEquals("管理员")) {
				request.getRequestDispatcher("managerScene.jsp").forward(request, response);
			}else if(identity.contentEquals("用户")) {
				String temp="统计网页访问次数";int tempnum=1;
				mapper.updateTimes(temp,tempnum);sqlsession.commit();
				request.getRequestDispatcher("mainScene.jsp").forward(request, response);
			}
		}else {
			request.getRequestDispatcher("messagePage.jsp?message=登录失败，请重新检查").forward(request, response);
		}
		//5.释放资源
		sqlsession.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
